//
//  EditViewController.m
//  Mid_Project
//
//  Created by 王世界 on 2020/12/2.
//

#import "EditViewController.h"
#import "Masonry.h"
#import "Single.h"
#import "AFNetworking.h"
@interface EditViewController ()

@end

@implementation EditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    self.title = self.title;
    self.edgesForExtendedLayout = UIRectEdgeBottom;
    
    _edit = [[UITextField alloc] init];
    _edit.backgroundColor = [UIColor whiteColor];
    
    self.edit.delegate = self;
    
    _edit.text = _string;
    _edit.font = [UIFont systemFontOfSize:22];
    _edit.clearButtonMode = UITextFieldViewModeAlways;
    _edit.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:_edit];
    [_edit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.width.equalTo(self.view.mas_width).multipliedBy(1);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.08);
    }];
    
    [_edit becomeFirstResponder];
    _edit.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOKbtn)];
        self.navigationItem.rightBarButtonItem = rightBarItem;
    
//    _edit.keyboardAppearance = UIKeyboardAppearanceDefault;
    // Do any additional setup after loading the view.
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES]; }

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    //当用户按下ruturn，把焦点从textField移开那么键盘就会消失了

    [textField resignFirstResponder];

    return YES;

}

- (void)onClickedOKbtn {
    NSString *tem = _edit.text;
    NSMutableDictionary * a=[NSMutableDictionary dictionaryWithDictionary:[Single sharedInstance].datalist1[0]];
    NSString *url2;
    NSDictionary *parameters;
    if ([_label isEqualToString:@"Info"]) {
        NSDictionary *Info = a[@"info"];
        url2 = @"http://172.18.178.56/api/user/info";
        [Info setValue:tem forKey:@"Bio"];
        parameters = @{@"Avatar":a[@"Info"][@"Avatar"],@"Bio":tem,@"Gender":a[@"Info"][@"Gender"],@"Name":a[@"Info"][@"Name"]};
    }
    if ([_label isEqualToString:@"Name"]) {
        url2 = @"http://172.18.178.56/api/user/name";
        parameters = @{@"name":tem};
        [a setValue:tem forKey:_label];
    }
    [Single sharedInstance].datalist1[0] = a;
    AFHTTPSessionManager *manage2 = [AFHTTPSessionManager manager];
    // 设置请求体为JSON
    manage2.requestSerializer = [AFJSONRequestSerializer serializer];
    // 设置响应体为JSON
    manage2.responseSerializer = [AFJSONResponseSerializer serializer];
    [manage2 POST:url2 parameters:parameters headers:nil progress:^(NSProgress * _Nonnull uploadProgress){} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"success");
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
